<?php

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';
require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
require_once ROOT_PATH . '/lib/common/SearchObject.php';
require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';

require_once ROOT_PATH . '/lib/models/skillmatrix/SkillUser.php';



class SkillExpertise {

	const TABLE_NAME = 'hs_hr_skill_expertise';
	
	const DB_FIELD_SKILL_EXPERTISE_ID = 'ID';
	const DB_FIELD_SKILL_EXPERTISE_NAME = 'ExpertiseName';	
	const DB_FIELD_SKILL_EXPERTISE_GROUP_ID = 'SkillGroupID';
	const DB_FIELD_SKILL_EXPERTISE_ORDER = 'ordering';	
	
	private $skill_expertise_id;
	private $skill_expertise_name;
	private $skill_expertise_group_id;
	private $skill_expertise_order;
	
	
	public function __construct($id = null) {
		$this->skill_expertise_id = $id;
	}

	public function setId($id) {
		$this->skill_expertise_id = $id;
	}
	
	public function getId() {
		return $this->skill_expertise_id;
	}
	
	public function setSkillExpertiseName($name) {
		$this->skill_expertise_name = $name;
	}
	
	public function getSkillExpertiseName() {
		return $this->skill_expertise_name;
	}
	
	public function setSkillGroupID($groupID){
		$this->skill_expertise_group_id = $groupID;
	}
	
	public function getSkillGroupID(){
		return $this->skill_expertise_group_id;
	}
	
	public function setSkillExpertiseOrder($oder){
		$this->skill_expertise_order= $oder;
	}
	public function getSkillExpertiseOrder(){
		return $this->skill_expertise_order;
	}

	

    public function save() {
		if (isset($this->skill_expertise_id)) {
			return $this->_update();
		} else {
			return $this->_insert();
		}
    }

	public static function delete($ids) {

		$count = 0;
		if (!is_array($ids)) {
			throw new SkillExpertiseException("Invalid parameter to delete(): ids should be an array", SkillExpertiseException::INVALID_PARAMETER);
		}
		foreach ($ids as $id) {
			if (!CommonFunctions::isValidId($id)) {
				throw new SkillExpertiseException("Invalid parameter to delete(): id = $id", SkillExpertiseException::INVALID_PARAMETER);
			}
		}
		if (!empty($ids)) {
			
			$sql = sprintf("DELETE FROM %s WHERE %s IN (%s)", self::TABLE_NAME,
			                self::DB_FIELD_SKILL_EXPERTISE_ID, implode(",", $ids));
			//print_r($sql);
			$conn = new DMLFunctions();
			$result = $conn->executeQuery($sql);
			if ($result) {
				$count = mysql_affected_rows();
			}
		}
		return $count;		

	}	

	public static function getAll() {
		return self::_getList();
	}
	
	public static function getUserSkills($id,$userID) {		
		return SkillUser::getSkillUserByExpertise($id,$userID);
	}
	
	public static function getSkillExpertiseByGroup($groupid) {
		$selectCondition[0] =  self::DB_FIELD_SKILL_EXPERTISE_GROUP_ID." = $groupid"; 
		return self::_getList($selectCondition);
	}
	
	public static function getSkillExpertise($skillExpertiseID) {
		$selectCondition[0] = "(". self::DB_FIELD_SKILL_EXPERTISE_ID ." = ". $skillExpertiseID." )";		
		$skillExpertiseLists = self::_getList($selectCondition);
		if(count($skillExpertiseLists)>0){
			return $skillExpertiseLists[0];
		}else{
			$skillExpertise = new SkillExpertise();
			return $skillExpertise;
		}
	}

	private static function _getList($selectCondition = null,$selectOrderBy='ordering',$selectOrder=null, $selectLimit=null) {
		$fields[0] = self::DB_FIELD_SKILL_EXPERTISE_ID;
		$fields[1] = self::DB_FIELD_SKILL_EXPERTISE_NAME;
		$fields[2] = self::DB_FIELD_SKILL_EXPERTISE_GROUP_ID;				
		$fields[3] = self::DB_FIELD_SKILL_EXPERTISE_ORDER;			
		
		$tables[0] = self::TABLE_NAME ;
		
		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->simpleSelect($tables[0],$fields,$selectCondition,$selectOrderBy,$selectOrder, $selectLimit);
		//die($sql);
		$actList = array();

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		while ($result && ($row = mysql_fetch_assoc($result))) {
			$actList[] = self::_createFromRow($row);
		}
		return $actList;
	}

	private function _insert() {
				
		$fields[0] = self::DB_FIELD_SKILL_EXPERTISE_NAME;
		$fields[1] = self::DB_FIELD_SKILL_EXPERTISE_GROUP_ID;
		$fields[2] = self::DB_FIELD_SKILL_EXPERTISE_ORDER;
		
		$values[0] = "'{$this->skill_expertise_name}'";
		$values[1] = $this->skill_expertise_group_id;		
		$values[2] = $this->skill_expertise_order;	
		
		
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_insert = 'true';
		$sqlBuilder->arr_insert = $values;
		$sqlBuilder->arr_insertfield = $fields;

		$sql = $sqlBuilder->addNewRecordFeature2();

		$conn = new DMLFunctions();

		$result = $conn->executeQuery($sql);
		if (!$result || (mysql_affected_rows() != 1)) {
			throw new OptionsException("Insert failed. ", OptionsException::DB_ERROR);
		}

		return $this->skill_expertise_id;
	}

	private function _update() {

		$fields[0] = self::DB_FIELD_SKILL_EXPERTISE_ID;
		$fields[1] = self::DB_FIELD_SKILL_EXPERTISE_NAME;
		$fields[2] = self::DB_FIELD_SKILL_EXPERTISE_GROUP_ID;
		$fields[3] = self::DB_FIELD_SKILL_EXPERTISE_ORDER;
				

		$values[0] = $this->skill_expertise_id;
		$values[1] = "'{$this->skill_expertise_name}'";	
		$values[2] = $this->skill_expertise_group_id;
		$values[3] = $this->skill_expertise_order;			
				
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_update = 'true';
		$sqlBuilder->arr_update = $fields;
		$sqlBuilder->arr_updateRecList = $values;

		$sql = $sqlBuilder->addUpdateRecord1(0);

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		if (!$result) {
			throw new OptionsException("Update failed. SQL=$sql", OptionsException::DB_ERROR);
		}
		return $this->skill_expertise_id;
	}

    private static function _createFromRow($row) {
    	
    	$skillExpertise = new SkillExpertise($row[self::DB_FIELD_SKILL_EXPERTISE_ID]);
    	$skillExpertise->setId($row[self::DB_FIELD_SKILL_EXPERTISE_ID]);
    	$skillExpertise->setSkillExpertiseName($row[self::DB_FIELD_SKILL_EXPERTISE_NAME]);
    	$skillExpertise->setSkillGroupID($row[self::DB_FIELD_SKILL_EXPERTISE_GROUP_ID]);
    	$skillExpertise->setSkillExpertiseOrder($row[self::DB_FIELD_SKILL_EXPERTISE_ORDER]);
    	
    	return $skillExpertise;
    }
   

}

class SkillExpertiseException extends Exception {
	const INVALID_PARAMETER = 0;
	const DB_ERROR = 1;
}
?>

